package com.treetao.union.mall.api.base;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.treetao.union.core.exception.DevMException;
import com.treetao.union.core.redis.RedisService;
import com.treetao.union.core.tencent.microapp.option.MicroappOption;
import com.treetao.union.core.util.JwtTokenUtil;
import com.treetao.union.db.pojo.SettingTypeWeixin;
import com.treetao.union.mall.api.service.TencentDbService;

public class BaseApiController {
	@Autowired
	protected TencentDbService tencentDbService;
	@Autowired
	protected RedisService redisService;
	
	/**
	 * 获取微信配置
	 * @param appid
	 * @return
	 */
	protected MicroappOption initTencentWxConfig() {
		SettingTypeWeixin weixin = tencentDbService.getConfigTencentWeixin();
		MicroappOption option = new MicroappOption();
		if(weixin==null) return null;
		option.setAppId(weixin.getWxAppid());
		option.setSecret(weixin.getWxSecret());
		return option;
	}
	
//	protected TencentSmsOption initTencentSmsConfig(String sno) {
//		String key = "tencent-sms-config-"+sno;
//		TencentSms sms = null;
//		if(redisService.hasKey(key)) {
//			sms = (TencentSms)redisService.get(sno);
//			TencentSmsOption option = new TencentSmsOption();
//			option.setAppid(sms.getAppid());
//			option.setAppkey(sms.getAppkey());
//			option.setSign(sms.getSign());
//			option.setTpl_id(sms.getTplid());
//			option.setRefresh(false);
//			return option;
//		} else {//重新获取 
//			sms = tencentDbService.getConfigTencentSms(sno);
//			redisService.set(key, sms, 30*60);//缓存30分钟
//			TencentSmsOption option = new TencentSmsOption();
//			option.setAppid(sms.getAppid());
//			option.setAppkey(sms.getAppkey());
//			option.setSign(sms.getSign());
//			option.setTpl_id(sms.getTplid());
//			option.setRefresh(true);
//			return option;
//		}
//	}
	
	/**
	 * 获取openid
	 * @return
	 */
	protected String getOpenidFromToken() {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		String auth = request.getHeader("Authorization");
		if(JwtTokenUtil.isTokenExpired(auth)) {
			throw new DevMException(999, "登录超时");
		}
		String token = JwtTokenUtil.getSubject(auth);
		if(token.contains("@")) {
			return token.split("@")[0];
		}
		return null;
	}
	/**
	 * 获取wxuid
	 * @return
	 */
	protected Long getWxuidFromToken() {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		String auth = request.getHeader("Authorization");
		if(JwtTokenUtil.isTokenExpired(auth)) {
			throw new DevMException(999, "登录超时");
		}
		String token = JwtTokenUtil.getSubject(auth);
		if(token.contains("@")) {
			return Long.valueOf(token.split("@")[1]);
		}
		return null;
	}
	
	protected <T> QueryWrapper<T> getQueryWrapperNoDel() {
		QueryWrapper<T> q = new QueryWrapper<>();
		q.eq("del_flag", false);
		return q;
	}
}
